<?php
class CatalogCustomFieldValue extends CActiveRecord
{
	public static function model($className=__CLASS__)
	{
            return parent::model($className);
	}

	public function tableName()
	{
            return 'CatalogCustomFieldValue';
	}

	public function rules()
	{

            return array(
			array('id, c_c_f_id, value, c_id', 'safe'),
		);
	}
        
	public function relations()
	{
            return array(
			'c' => array(self::BELONGS_TO, 'Catalog', 'c_id'),
			'cCF' => array(self::BELONGS_TO, 'CatalogCustomFields', 'c_c_f_id'),
		);
	}

	public function attributeLabels()
	{
            return array(
                    'id' => Yii::t('system','ID'),
                    'c_c_f_id' => Yii::t('system','C C F'),
                    'value' => Yii::t('system','Value'),
                    'c_id' => Yii::t('system','C'),
            );
	}
        
        public function dropDownItemsAll(){
            $items= Array();
            $model = self::model()->findAll();
            foreach ($model as $value) {
                $items[$value->id] = $value->id;
            }
            return $items;
        }
        
        public static function getFieldsAndValueOfCategoryDefaultLanguage($catalog_id){
            $dataReader=Yii::app()->db->createCommand()
                ->select('f.label, c.value,c.id')
                ->from('CatalogCustomFieldValue c')
                ->join('CatalogCustomFields f', 'c.c_c_f_id = f.id')
                ->where('c.c_id=:catalog_id',array(':catalog_id' => $catalog_id))
                ->queryAll();
            return $dataReader;
        }
        
	public function search()
	{	
            $criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('c_c_f_id',$this->c_c_f_id);
		$criteria->compare('value',$this->value,true);
		$criteria->compare('c_id',$this->c_id);

            return new CActiveDataProvider($this, array(
                    'criteria'=>$criteria,
            ));
	}
}


//    public static function allLang(){
//        $dataReader=Yii::app()->db->createCommand()
//        ->select('l,name')
//        ->from(array('Language'))
//        ->queryAll();
//        return $dataReader;
//    }
//    
//    public static function countLang(){
//        $sql = "SELECT COUNT(*) as count FROM `Language`";
//        $dataReader=Yii::app()->db->createCommand($sql)->queryRow();
//        return $dataReader['count'];
//    }
//    
//    public static function allExcept($except_id){
//        $dataReader=Yii::app()->db->createCommand()
//        ->select('*')
//        ->from(array('Language'))
//        ->where(array('and', '`primary` <> :primary', array('not in','id',$except_id)),array(':primary'=>'1'))
//        ->queryAll();
//        return $dataReader;
//    }